package com.voicechat.firstmet.adapter;

import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.voicechat.firstmet.App;
import com.voicechat.firstmet.R;
import com.voicechat.firstmet.client.OssConfig;
import com.voicechat.firstmet.fragment.IMSendGifFragment;
import com.voicechat.firstmet.realm.ListBean;
import com.voicechat.firstmet.utils.ConstantSet;
import com.voicechat.firstmet.utils.FileUtils;
import com.voicechat.firstmet.utils.GlideLoadUtils;
import com.voicechat.firstmet.utils.Utils;

import java.util.ArrayList;
import java.util.List;

/**
 * @ProjectName: FirstMet
 * @Package: com.voicechat.firstmet.adapter
 * @ClassName: MsgGifGridViewAdapter
 * @Description: java类作用描述
 * @Author: ljp
 * @CreateDate: 2018/11/13 10:28
 * @Version: 1.0
 */
public class MsgGifGridViewAdapter extends BaseAdapter {
    private List<ListBean> dataList;
    private boolean isCheckd = false;
    private int position = -1;
    private ItemClick itemClick;
    private String TAG = "MsgGifGridViewAdapter";
    public void setItemClick(ItemClick itemClick) {
        this.itemClick = itemClick;
    }

    public boolean isCheckd() {
        return isCheckd;
    }

    public void setCheckd(boolean checkd,int position) {
        this.position = position;
        isCheckd = checkd;
    }
    public void setClearCheckd(){
        isCheckd = false;
    }
    public MsgGifGridViewAdapter(List<ListBean> datas, int page) {
        dataList = new ArrayList<ListBean>();
        //start end分别代表要显示的数组在总数据List中的开始和结束位置
        int start = page * IMSendGifFragment.gridNum;
        int end = start + IMSendGifFragment.gridNum;
        while ((start < datas.size()) && (start < end)) {
            dataList.add(datas.get(start));
            start++;
        }
        Log.i(TAG, "MsgGifGridViewAdapter: -------"+dataList.size());
    }

    @Override
    public int getCount() {
        return dataList.size();
    }

    public List<ListBean> getDataList() {
        return dataList;
    }

    @Override
    public ListBean getItem(int i) {
        return dataList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View itemView, ViewGroup viewGroup) {
        ViewHolder mHolder;
        if (itemView == null) {
            mHolder = new ViewHolder();
            itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_grid_msg_gif, viewGroup, false);
            itemView.setTag(mHolder);
            mHolder.imageGif = itemView.findViewById(R.id.image_gif);
            mHolder.tvGiftGold = itemView.findViewById(R.id.tv_gold);
            mHolder.tvGiftName = itemView.findViewById(R.id.tv_gif_name);
        } else {
            mHolder = (ViewHolder) itemView.getTag();
        }
        ListBean listBean = getItem(i);
        //得到礼物的名字
        if(!TextUtils.isEmpty(listBean.getImgUrl())){
            String voiceName = Utils.getVoiceName(listBean.getImgUrl());
            String s = ConstantSet.VOICEINTR + ConstantSet.GIF_PATH + voiceName;
            //判断文件是否存在
            if(FileUtils.isExists(s)){
                GlideLoadUtils.getInstance().glideLoad(App.getContext(),s,mHolder.imageGif);
            }else {
                GlideLoadUtils.getInstance().glideLoad(App.getContext(), OssConfig.OSS_HTTP+listBean.getImgUrl(),
                        mHolder.imageGif);
            }
        }
        mHolder.tvGiftGold.setText(listBean.getGold()+"");
        mHolder.tvGiftName.setText(listBean.getName());
        if(this.position == i && isCheckd){
            itemView.setBackgroundResource(R.drawable.red_on);
            //this.position = -1;
            //isCheckd = false;
        }else {
            itemView.setBackgroundResource(0);
        }

        View finalItemView = itemView;
        /*itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                itemClick.setItemClick(i);
            }
        });*/
        return itemView;
    }

     class ViewHolder {
        private ImageView imageGif;
        private TextView tvGiftName;
         private TextView tvGiftGold;
        private LinearLayout linearBack;
     }
    public interface ItemClick{
        void setItemClick(int position );
     }
}